//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package com.chris.serve;

import com.alibaba.fastjson.JSONObject;
import com.chris.entity.reader;
import com.chris.impl.loanImpl;
import com.chris.impl.readerImpl;
import com.chris.impl.registerImpl;
import com.chris.utils.DbConnection;
import com.chris.utils.JsonProcess;
import com.chris.utils.LogUtil;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Date;
import java.sql.SQLException;
import java.sql.Time;
import java.time.*;
import javax.servlet.ServletException;
import javax.servlet.ServletInputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet({"/borrow"})
public class borrow extends HttpServlet {
    private final loanImpl dao = new loanImpl();
    private final readerImpl dao2 = new readerImpl();
    private final registerImpl dao3 = new registerImpl();

    public borrow() {
    }

    public void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException {
        DbConnection.begin();
        ServletInputStream inputStream ;
        JSONObject read ;
        PrintWriter writer = null;
        String reader_id ;
        int journal_id ;
        long borrow_time ;
        try {
             inputStream = req.getInputStream();
             read = JsonProcess.read(inputStream);
             writer = resp.getWriter();
             reader_id = (String)read.get("account");
             journal_id= (Integer)read.get("journal_id");
             borrow_time= (Long)read.get("borrow_time");
        }
        catch (IOException e){
            e.printStackTrace();
            writer.print(JsonProcess.respError("参数错误"));
            LogUtil.log("参数错误",e);
            DbConnection.rollBack();
            return;
        }
        try {
            int num = this.dao2.getUserByAccount(reader_id).getBookNum();
            if (num > 0) {
                --num;
                this.dao2.setReaderNum(num, reader_id);
                Date date = new Date(borrow_time);
                LocalDate localDate = date.toLocalDate().plusDays(30L);
                ZonedDateTime zonedDateTime = localDate.atStartOfDay(ZoneId.systemDefault());
                java.util.Date date1 = Date.from(zonedDateTime.toInstant());
                Date date2 = new Date(date1.getTime());
                this.dao.insert(reader_id, journal_id, date, date2);
                this.dao3.Borrowed(journal_id, 1);
                reader userByAccount = dao2.getUserByAccount(reader_id);
                int bookNum = userByAccount.getBookNum();
                JSONObject jsonObject = new JSONObject();
                jsonObject.put("bookNum",bookNum);
                jsonObject.put("date",date2);
                DbConnection.commit();
                writer.print(JsonProcess.respSuccess("借阅成功", jsonObject));
            } else {
                DbConnection.rollBack();
                writer.print(JsonProcess.respError("已超过最大借阅数量"));
            }
        }
        catch (SQLException e){
            e.printStackTrace();
            writer.print(JsonProcess.respError("当天内不能重复借书"));
            LogUtil.log("当天内不能重复借书",e);
            DbConnection.rollBack();
        }
    }
    public void doOptions(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.addHeader("Access-Control-Allow-Headers", "*");
    }
}
